﻿// /////////////////////////////////////////////////////////////////////
// 
//  Codeplex JSAnalyse
//  Copyright (c) 2011
//  JSAnalyse.Test   DependencyRunner.cs
// 
// /////////////////////////////////////////////////////////////////////
using System.Collections.Generic;
using JSAnalyse.Core;
using JSAnalyse.Test.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSAnalyse.Test
{
    /// <summary>
    /// This is a test class for testContextInstance and is intended
    /// to contain all testContextInstance Unit Tests
    /// </summary>
    [TestClass]
    public class ObjectLiteralSyntaxTest
    {
        /// <summary>
        /// A simple dependency check test
        /// </summary>
        [TestMethod]
        public void SimpleDependencyTest()
        {
            var mainJs = "Main.js";
            var ref1Js = "Ref1.js";
            var ref2Js = "Ref2.js";

            var allowedDependencies = new List<AllowedDependencyTest>
                                          {
                                              new AllowedDependencyTest(ref1Js, mainJs),
                                              new AllowedDependencyTest(ref2Js, mainJs)
                                          };

            var fileInfoMain = new JavascriptFileInfo(mainJs,
                                                      @"var main = {
                                                                    type: 'test',
                                                                    function1: function () {
                                                                        return this.type;
                                                                    }
                                                                }");
            var fileInfoRef1 = new JavascriptFileInfo(ref1Js,
                                                      @"var ref1 = {
                                                                    function2: function () {
                                                                        return main.function1();
                                                                    }
                                                                }");
            var fileInfoRef2 = new JavascriptFileInfo(ref2Js,
                                                      @"var ref2 = {
                                                                    function3: function () {
                                                                        var test = main.function1();
                                                                        var test2 = ref1.function2();
                                                                        return test + test2;
                                                                    }
                                                                }");

            var dependencyRunner = new DependencyRunner(
                new List<JavascriptFileInfo> {fileInfoMain, fileInfoRef1, fileInfoRef2},
                new AllowedDependenciesTest(allowedDependencies)
                );

            var result = dependencyRunner.Start();
            var mainResult = GeneralHelper.GetResultItem(result, mainJs);
            var ref1Result = GeneralHelper.GetResultItem(result, ref1Js);
            var ref2Result = GeneralHelper.GetResultItem(result, ref2Js);
            Assert.AreEqual(3, result.Count);

            Assert.IsNotNull(mainResult);
            Assert.AreEqual(0, mainResult.WrongFileDependencies.Count);

            Assert.IsNotNull(ref1Result);
            Assert.AreEqual(0, ref1Result.WrongFileDependencies.Count);

            Assert.IsNotNull(ref2Result);
            Assert.AreEqual(1, ref2Result.WrongFileDependencies.Count);
            Assert.AreEqual(4, ref2Result.WrongFileDependencies[0].DependencySourceLocation.Line);
        }
    }
}
